package com.interview.javabase.algorithm;

import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;

/**
 * @description: 排序算法
 * @author: zhangshuguang@vichain.com
 * @date: 2019/06/12 15:19
 */
@SpringBootTest
public class Demo13 {

    @Test
    public void solution1() {
        int[] arr = {2, 9, 1, 0, 4, 3, 6};
        insertSort(arr, arr.length);
        for (int i : arr) {
            System.out.println(i);
        }
    }

    private void insertSort(int[] arr, int length) {
        for (int i = 1; i < arr.length - 1; i++) {

            // 每次都取一个数作为对比的值
            int temp = arr[i];
            int j;
            for (j = i; j > 0 && arr[j - 1] > temp; j--) {
                arr[j] = arr[j - 1];
            }
            arr[j] = temp;
        }
    }

}
